package com.my.geojson.test.geoserver;


import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy;
import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;

import java.io.File;
import java.net.URL;

public class test {

    public static void main(String[] args) throws Exception {
        String geoserverUrl = "http://localhost:8080/geoserver";
        String geoserverUsername = "admin";
        String geoserverPassword = "geoserver";

        GeoServerRESTManager manager = new GeoServerRESTManager(new URL(geoserverUrl), geoserverUsername,
                geoserverPassword);
        GeoServerRESTPublisher geoServerRESTPublisher = manager.getPublisher();
        GeoServerRESTReader geoServerRESTReader = manager.getReader();

        String workspace = "xiaozhai";
        String storeName = "3857";
        String coverageName = "3857";
        String style = "nlzh";

        boolean workspaceNull = geoServerRESTReader.existsWorkspace(workspace);
        System.out.println("workspace是否存在：" + workspaceNull);
        if(!workspaceNull) {
            workspaceNull = geoServerRESTPublisher.createWorkspace(workspace);
            System.out.println("workspace是否创建成功：" + workspaceNull);
        }

        String filePath = "F:\\GeoServerDatabase\\Community\\Community.shp";
        String zipPath = "F:\\GeoServerDatabase\\Community\\Community.zip";
        //創建shp數據存儲
        GSShapefileDatastoreEncoder gsShapefileDatastoreEncoder= new
                GSShapefileDatastoreEncoder(storeName,new URL("file:" + filePath));
        boolean createStore = manager.getStoreManager().create(workspace, gsShapefileDatastoreEncoder);
        System.out.println("create shp store " + createStore);
        //發布shp圖層（如果沒有目標數據存儲則會先自動創建該命名數據存儲再發布）
        boolean publish = manager.getPublisher().publishShp(workspace, storeName, "Community",new File(zipPath));
        System.out.println("publish shp " + publish);
        //讀取目標矢量數據存儲
        //RESTDataStore restDataStore = manager.getReader().getDatastore(workSpace, storeName);

        //初始化和構造栅格數據儲存
        String srs = "EPSG";//SRS空間參考系統 crs坐標參考系統
        GSCoverageEncoder gsCoverageEncoder = new GSCoverageEncoder();
        gsCoverageEncoder.setName("test");
        gsCoverageEncoder.setTitle("test");
        gsCoverageEncoder.setSRS(srs);
        gsCoverageEncoder.setNativeFormat("GeoTIFF");
        gsCoverageEncoder.addSupportedFormats("GEOTIFF");
        gsCoverageEncoder.addKeyword("geoTiff");
        gsCoverageEncoder.addKeyword("WCS");
        gsCoverageEncoder.setNativeCRS(srs);
        gsCoverageEncoder.setRequestSRS(srs);
        gsCoverageEncoder.setResponseSRS(srs);
//        gsCoverageEncoder.setProjectionPolicy(REPROJECT_TO_DECLARED);
        gsCoverageEncoder.setLatLonBoundingBox(-180, -90, 180, 90, "EPSG:4326");
        //創建栅格數據存儲
        createStore = manager.getPublisher().createCoverage(workspace,storeName,gsCoverageEncoder);
        System.out.println("Coverage store " + createStore);
        //待發布GeoTIFF數據路徑
        filePath = "F:\\GeoServerDatabase\\test.tif";
        //發布GeoTIFF（如果沒有目標數據存儲則會先自動創建該命名栅格數據存儲再發布）
        publish = manager.getPublisher().publishGeoTIFF(workspace, storeName,new File(filePath));
        System.out.println("publish Coverage " + publish);
        //讀取目標栅格數據存儲
        //RESTCoverageStore restCoverageStore = manager.getReader().getCoverageStore(workSpace, storeName);


        filePath = "C:\\Users\\Administrator\\Desktop\\a\\3857.tif";
        boolean styleNull = geoServerRESTReader.existsStyle(style);
        System.out.println("style是否存在：" + styleNull);
        boolean layerNull = geoServerRESTReader.existsLayer(workspace, coverageName);
        System.out.println("layer是否存在：" + layerNull);
        if(!layerNull) {
            layerNull = geoServerRESTPublisher.publishGeoTIFF(workspace, storeName, coverageName,
                    new File(filePath), "EPSG:3857", ProjectionPolicy.FORCE_DECLARED, style, null);
            System.out.println("layer是否创建成功：" + layerNull);
        }


        boolean coveragestoreNull = geoServerRESTReader.existsCoveragestore(workspace, storeName);
        System.out.println("coveragestore是否存在：" + coveragestoreNull);
        boolean coverageNull = geoServerRESTReader.existsCoverage(workspace, storeName, coverageName);
        System.out.println("coverage是否存在：" + coverageNull);

        boolean coverageResult = geoServerRESTPublisher.unpublishCoverage(workspace, storeName, coverageName);
        System.out.println("coverage删除成功：" + coverageResult);
        boolean coveragestoreResult = geoServerRESTPublisher.removeCoverageStore(workspace, storeName, true);
        System.out.println("coveragestore删除成功：" + coveragestoreResult);
    }

}